package com.guigu.qingcheng.orderservice.controller;


import com.guigu.qingcheng.orderservice.mapper.TbOrderMapper;
import com.guigu.qingcheng.orderservice.service.TbOrderService;
import com.guigu.qingcheng.pojo.order.TbOrder;
import com.guigu.qingcheng.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("tborder")
public class TbOrderController {

    @Autowired
    private TbOrderService tbOrderService;
    @Resource
    private TbOrderMapper mapper;

    /**
     * 根据用户名查询订单信息
     */
    @GetMapping("/findOrderByUsername")
    public R findOrderByUsername(@RequestParam("username")String username){
        List<TbOrder> list=tbOrderService.findOrderByUsername(username);
        return R.ok().data("rows",list);
    }

    @GetMapping("findById")
    public R findById (String id) {
        TbOrder tbOrder = tbOrderService.getById(id);
        if (tbOrder  == null)
            return R.error();
        return R.ok().data("order",tbOrder);
    }

    @GetMapping("findByOrderId")
    public R findByOrderId (String id) {
        return tbOrderService.findByOrderId(id);
    }

    /**
     * 查询未发货的订单
     * @param
     * @return
     */
    @GetMapping("findByConsignStatus")
    public R findByConsignStatus () {
        return tbOrderService.findByConsignStatus();
    }

    //查询分页查询
    @PostMapping("findPage")
    public R findPage (@RequestParam("page") Integer page, @RequestParam("size") Integer size,@RequestBody(required = false) Map<String,Object> searchMap ) {
        return tbOrderService.findPage(page, size, searchMap);
    }

    //新增
    @PostMapping("add")
    public R add (@RequestBody TbOrder tbOrder) {
        boolean save = tbOrderService.save(tbOrder);
        if (save)
            return R.ok();
        return R.error();
    }

    //修改
    @PostMapping("update")
    public R update (@RequestBody TbOrder tbOrder) {
        boolean save = tbOrderService.updateById(tbOrder);
        if (save)
            return R.ok();
        return R.error();
    }

    //取消订单(删除订单操作)
    @PostMapping("updateOrder")
    public R updateOrder (@RequestBody TbOrder tbOrder, @RequestParam("remarks")String remarks) {
        return tbOrderService.updateOrder(tbOrder,remarks);
    }

    //确认收货
    @PostMapping("updateqrsh")
    public R updateqrsh (@RequestBody TbOrder tbOrder) {
        return tbOrderService.updateqrsh(tbOrder);
    }

    //合并订单
    @PostMapping("hbOrder")
    public R hbOrder (@RequestParam("id") String id, @RequestParam("id1") String id1) {
        return tbOrderService.hbOrder(id,id1);
    }

    //一键收货
    @PostMapping("yjupdateqrsh")
    public R yjupdateqrsh (@RequestBody List<TbOrder> tbOrders) {
        return tbOrderService.yjupdateqrsh(tbOrders);
    }

    //删除
    @GetMapping("delete")
    public R delete (String id) {
        boolean save = tbOrderService.removeById(id);
        if (save)
            return R.ok();
        return R.error();
    }

    //多个id查询
    @GetMapping("findByIds")
    public R findByIds (String ids) {
        return tbOrderService.findById(ids);
    }

    /**
     * 批量发货
     * @param tbOrders
     * @return
     */
    @PostMapping("updates")
    public R updates (@RequestBody List<TbOrder> tbOrders) {
        return tbOrderService.updates(tbOrders);
    }

    /**
     * 批量关闭订单
     * @param tbOrders
     * @return
     */
    @PostMapping("updatedown")
    public R updatedown (@RequestBody List<TbOrder> tbOrders) {
        return tbOrderService.updatedown(tbOrders);
    }

    /**
     * 批量删除订单
     * @param tbOrders
     * @return
     */
    @PostMapping("updateDelete")
    public R updateDelete (@RequestBody List<TbOrder> tbOrders) {
        return tbOrderService.updateDelete(tbOrders);
    }


    /**
     * 商品表和商品库存表联表查询
     * @param id
     * @return
     */
    @GetMapping("spuandspk")
    public R SpuAnd (String id) {
        List<Map> maps = mapper.SpuAndSpk(id);
        for (Map map:maps) {
            Object items = map.get("spec_items");
            System.out.println(items);
        }
        return R.ok().data("list",maps);
    }

    /**
     * 保存订单
     * @param order
     * @return
     */
    @PostMapping("saveOrder")
    public Map<String,Object> saveOrder(@RequestBody TbOrder order ){
        String username = "覃仁斌";
        order.setUsername(username);
        return tbOrderService.add(order);
    }
}

